package Presentation;

import Logic.Encryptor;
import Logic.Engine;
import Logic.Movie;
import Logic.Rank;
import Logic.User;

public class FacadeUI implements MainFascade{
	
	private Engine mainEngine;
	

	public FacadeUI(Engine eng){
		this.mainEngine = eng;		
	}
	
	// this function return Movie object after adding new movie to the DB from the given values strings,
	// if an error occurs during the creation, null returned.
	public Movie addMovie(String[] values) {
		return this.mainEngine.addNewMovie(values);
	}

	// this function return User object after adding new user to the DB from the given values strings,
	// if an error occurs during the creation, null returned.
	public User addUser(String[] values) {
		return this.mainEngine.addNewUser(values);
	}

	// this function return the Movie object with the given name, if no such movie exist, null returned
	public Movie findMovieByName(String name) {
		for (Movie m:this.mainEngine.getDb().getMovieVector()) {
			if (m.getName().compareTo(name)==0)
				return m;
		}
		return null;
	}

	// this function return the User object with the given username, if no such user exist, null returned
	public User findUserByName(String username) {
		for (User u:this.mainEngine.getDb().getUserVector()){
			if (u.getUsername().compareTo(username)==0){
				return u;
			}
		}
		return null;
	}

	// this function returning the User object if the username and password are suitable for DB user.
	// otherwise null is returned
	public User login(String username, String password) {
		User tmp = this.findUserByName(username);
		if (tmp != null)
			if (tmp.isCorrectPassword(password))
				return tmp;
		return null;
	}

	// the function creates a Rank object from the given params, updating the User and the Movie object for the 
	// new rank.
	public void rankMovie(User user, Movie movie, int rank) {
		Rank tmp = new Rank(movie,user,rank);
		movie.addRank(tmp);
		user.addRank(tmp);
	}

	// the function remove the movie from the DB
	public void removeMovie(Movie movie) {
		this.mainEngine.removeMovie(movie);
	}

	// the function remove the user from the DB
	public void removeUser(User user) {
		this.mainEngine.removeUser(user);
	}
	
	public void shutDown(){
		mainEngine.shutDown();
	}

	@Override
	public void editMovieDetails(Movie movie, String[] vals) {
		this.mainEngine.editMovieDetails(movie, vals);
	}

	@Override
	public void editUserDetails(User user, String[] vals) {
		this.mainEngine.editUserDetails(user, vals);	
	}

}
